// 应用程序上下文继承
<template>
<span class="el-tag el-tag--primary el-tag--light"><span class="el-tag__content">&gt; 2.0.4</span><!--v-if--></span>
<p>现在 MessageBox 接受构造器的 <code>context</code> 作为第二个(如果你正在使用消息框变量的话) 参数，这个参数允许你将当前应用的上下文注入到消息中，这将允许你继承应用程序的所有属性。</p>
<div class="language-ts vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">ts</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light: #D73A49; --shiki-dark: #F97583;">import</span><span style="--shiki-light: #24292E; --shiki-dark: #E1E4E8;"> { getCurrentInstance } </span><span style="--shiki-light: #D73A49; --shiki-dark: #F97583;">from</span><span style="--shiki-light: #032F62; --shiki-dark: #9ECBFF;"> 'vue'</span></span>
<span class="line"><span style="--shiki-light: #D73A49; --shiki-dark: #F97583;">import</span><span style="--shiki-light: #24292E; --shiki-dark: #E1E4E8;"> { ElMessageBox } </span><span style="--shiki-light: #D73A49; --shiki-dark: #F97583;">from</span><span style="--shiki-light: #032F62; --shiki-dark: #9ECBFF;"> 'element-plus'</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light: #6A737D; --shiki-dark: #6A737D;">// 在你的 setup 方法中</span></span>
<span class="line"><span style="--shiki-light: #D73A49; --shiki-dark: #F97583;">const</span><span style="--shiki-light: #24292E; --shiki-dark: #E1E4E8;"> { </span><span style="--shiki-light: #005CC5; --shiki-dark: #79B8FF;">appContext</span><span style="--shiki-light: #24292E; --shiki-dark: #E1E4E8;"> } </span><span style="--shiki-light: #D73A49; --shiki-dark: #F97583;">=</span><span style="--shiki-light: #6F42C1; --shiki-dark: #B392F0;"> getCurrentInstance</span><span style="--shiki-light: #24292E; --shiki-dark: #E1E4E8;">()</span><span style="--shiki-light: #D73A49; --shiki-dark: #F97583;">!</span></span>
<span class="line"><span style="--shiki-light: #6A737D; --shiki-dark: #6A737D;">// 你可以像这样传递参数：</span></span>
<span class="line"><span style="--shiki-light: #6F42C1; --shiki-dark: #B392F0;">ElMessageBox</span><span style="--shiki-light: #24292E; --shiki-dark: #E1E4E8;">({}, appContext)</span></span>
<span class="line"><span style="--shiki-light: #6A737D; --shiki-dark: #6A737D;">// 或者正在使用不同的调用方式</span></span>
<span class="line"><span style="--shiki-light: #24292E; --shiki-dark: #E1E4E8;">ElMessageBox.</span><span style="--shiki-light: #6F42C1; --shiki-dark: #B392F0;">alert</span><span style="--shiki-light: #24292E; --shiki-dark: #E1E4E8;">(</span><span style="--shiki-light: #032F62; --shiki-dark: #9ECBFF;">'Hello world!'</span><span style="--shiki-light: #24292E; --shiki-dark: #E1E4E8;">, </span><span style="--shiki-light: #032F62; --shiki-dark: #9ECBFF;">'Title'</span><span style="--shiki-light: #24292E; --shiki-dark: #E1E4E8;">, {}, appContext)</span></span></code></pre></div>
</template>
<style scoped>
@import '../../assets/elementPlusDemo.css'
</style>